﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CLibArtreve.bll;

namespace WebAppArtreve.views.shared.components.arts
{
    public partial class WUCArtImage : System.Web.UI.UserControl
    {
        //VARIABLES
        private Int64 _artId;
        private string _artPictureName;
        private Boolean _usingQueryString;
        private Boolean imageInserted;
        private string _connectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ARTREVEConnectionString"].ConnectionString;
            }
        }

        //PROPERTIES
        public Int64 ArtId
        {
            get
            {
                return _artId;
            }
            set
            {
                _artId = value;
            }
        }
        public string ArtPictureName
        {
            get
            {
                return this.ViewState["ArtPictureName"] as string;
            }
            set
            {
                _artPictureName = value;
                this.ViewState["ArtPictureName"] = _artPictureName;
            }
        }
        public Boolean UsingQueryString
        {
            get
            {
                return _usingQueryString;
            }
            set
            {
                _usingQueryString = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {   
            imageInserted = false;

        }

        //GET THE URL FOR THE IMAGE (WEBCONFIG->DMSPATH + FILENAME)
        protected string GetRealImageURL(string argument)
        {
            if (ArtPictureName != null && ArtPictureName != "")
                argument = ArtPictureName;
            else
            {
                //if (argument != "")
                    this.ViewState["ArtPictureName"] = argument;
                /*else
                    return ConfigurationManager.AppSettings["DMSPATH"] + "noimage.jpg";*/
            }

            imageInserted = true;
            return ConfigurationManager.AppSettings["DMSPATH"] + argument;
        }

        //VOORDAT SELECT IN ODS WORDT UITGEVOERD
        protected void ODSArtsALL_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            //int count = Request.QueryString.Count;            
            //count > 0
            if (Request.QueryString["Id"] == null && ArtId == 0)
            {
                Int64 toInt64 = Convert.ToInt64(-1);
                e.InputParameters["Id"] = toInt64;                  
            }
            else
            {
                if (UsingQueryString)
                {
                    e.InputParameters["Id"] = Request.QueryString["Id"];

                }
                else
                {
                    e.InputParameters["Id"] = ArtId;
                }
            }            
        }
        public void Rebind()
        {
            this.ListViewArt.DataBind();            
        }

        protected void ListViewArt_PreRender(object sender, EventArgs e)
        {
            if (!imageInserted)
            {
                GetRealImageURL("noimage.jpg");
            }
        }

        protected void ODSArtImagesALL_OnObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            //STEP 2. CREATE BLL
            ArtPictureBLL ObjectInstance = new ArtPictureBLL(_connectionString);
            //STEP 3. CONNECT TYPENAME
            e.ObjectInstance = ObjectInstance;
        }

    }
}